﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.BodyWeight;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepWeightCommands
{
    public class SleepWeightSearchHandler : IRequestHandler<SleepWeightSearchCommand, ResultT<SleepWeightDto>>
    {
        private readonly IBaseRepository<SleepWeight> SleepWeightbaseRepository;
        private readonly IBaseRepository<SleepInfor> SleepInforbaseRepository;
        private readonly IBaseRepository<WeightTypes> WeightTypesRepository;
        private readonly IBaseRepository<Variety> VarietybaseRepository;

        public SleepWeightSearchHandler(IBaseRepository<SleepWeight> sleepWeightbaseRepository, IBaseRepository<SleepInfor> SleepInforbaseRepository, 
            IBaseRepository<WeightTypes> WeightTypesRepository, IBaseRepository<Variety> VarietybaseRepository)
        {
            this.SleepInforbaseRepository=SleepInforbaseRepository;
            this.SleepWeightbaseRepository = sleepWeightbaseRepository;
            this.WeightTypesRepository = WeightTypesRepository;
            this.VarietybaseRepository = VarietybaseRepository;
        }
        /// <summary>
        /// 羊只称重查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultT<SleepWeightDto>> Handle(SleepWeightSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultT<SleepWeightDto>() { Code = (int)CodeEnum.search };
            var list = (from a in SleepWeightbaseRepository.GetAll()
                        join b in SleepInforbaseRepository.GetAll() on a.EarNumber equals b.EarNumber
                        join c in WeightTypesRepository.GetAll() on a.WeightType equals c.WeightType
                        join d in VarietybaseRepository.GetAll() on b.VarietyId equals d.VarietyId
                        select new SleepWeightDto()
                        {
                            SleepWeightId = a.SleepWeightId,
                            WeightDate = a.WeightDate,
                            EarNumber = a.EarNumber,
                            WeightType = a.WeightType,
                            WeightTypeName = c.WeightTypeName,
                            Weight = a.Weight,
                            Gender = b.Gender,
                            VarietyId = b.VarietyId,
                            VarietyName = d.VarietyName,
                            MonthAge=(DateTime.Now.Month-a.WeightDate.Month),
                            CreateId=a.CreateId,
                            CreateTime=a.CreateTime,
                            UpdateId=a.UpdateId,
                            UpdateTime=a.UpdateTime,
                        }).ToList();
            if (request.StartDate != null)
            {
                list = list.Where(x => x.WeightDate >= request.StartDate).ToList();
            }
            if (request.EndDate != null)
            {
                list = list.Where(x => x.WeightDate <= request.EndDate).ToList();
            }
            if (!string.IsNullOrEmpty(request.EarNumber))
            {
                list = list.Where(x => x.EarNumber.Contains(request.EarNumber)).ToList();
            }
            if (request.WeightType != 0)
            {
                list = list.Where(x => x.WeightType == request.WeightType).ToList();
            }
            if (request.VarietyId != 0)
            {
                list = list.Where(x => x.VarietyId == request.VarietyId).ToList();
            }
            if (request.Gender != 0)
            {
                list = list.Where(x => x.Gender == request.Gender).ToList();
            }
            result.Data = list;
            return result;

        }
    }
}
